<?php
session_start();
header("content-type:text/html;charset=utf-8");
include_once("sinasql.php");
include_once("request.php");

require_once("sinasql.php");

$code = $_GET["code"];

// 得到access_token 和openid
$response = getTokenInfo($code);
$obj = json_decode($response);
$access_token = $obj->access_token;
$openid = $obj->openid;
$_SESSION["openid"] = 'aaaaaaaaaaa';


print_r($_SESSION);

// 获取用户
	$userinfo = getUserInfo($access_token,$openid);
	$userObj = json_decode($userinfo);
//把用户插入到数据库中
  echo addUserInfo($userObj);

echo "<script>window.location.href='http://xiaojidunmogu.applinzi.com/share.html'</script>";


	// 获取access_token
function getTokenInfo($code){
	$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9304369530ffc11d&secret=31977f083f40a72c9c2faa100c487ebb&code={$code}&grant_type=authorization_code";
	 return httpGet($url);
	}


//获取用户信息
function getUserInfo($access_token,$openid){
$url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
return httpGet($url);


}










 ?>
